package com.hao.mydownloadlist.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.v7.app.NotificationCompat;
import android.widget.RemoteViews;

import com.hao.mydownloadlist.MainActivity;
import com.hao.mydownloadlist.R;
import com.hao.mydownloadlist.service.utils.ListUtils;
import com.hao.mydownloadlist.service.utils.MapUtils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hao on 16/11/22.
 */
public class NotificationUtils {

    private NotificationManager notificationManager;
    private Notification notification;
    private RemoteViews contentView;
    private ContextWrapper context;
    private int notification_id;

    private static Map<Integer, NotificationUtils> notificationUtilsList;


    public static Map<Integer, NotificationUtils> getNotificationUtilsList() {
        if (MapUtils.isEmpty(notificationUtilsList))
            notificationUtilsList = new HashMap<>();
        return notificationUtilsList;
    }

    public NotificationUtils(ContextWrapper context, int notification_id) {
        this.context = context;
        this.notification_id = notification_id;
    }

    /**
     * 创建通知
     *
     * @param name   文件的名字
     * @param iconId 显示的图片
     */
    public void creatNotfication(String name, int iconId) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context.getBaseContext());
        notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        builder.setSmallIcon(iconId);
        builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), iconId));
        builder.setAutoCancel(false);
        builder.setOngoing(true);
        contentView = new RemoteViews(context.getPackageName(), R.layout.layou_notifiction_item);
        contentView.setImageViewResource(R.id.notificationImage, iconId);
        contentView.setTextViewText(R.id.notificationTitle, name + "正在下载");
        contentView.setTextViewText(R.id.notificationPercent, "0%");
        contentView.setProgressBar(R.id.notificationProgress, 100, 0, false);
        builder.setContent(contentView);
        builder.setTicker("开始下载");
        notification = builder.build();
        Intent updateIntent = new Intent(context, MainActivity.class);
        updateIntent.getPackage();
        updateIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, updateIntent, 0);
        notification.contentIntent = pendingIntent;
        notificationManager.notify(notification_id, notification);
    }

    /**
     * 创建通知
     *
     * @param name    文件的名字
     * @param iconUrl 显示的网络图片
     */
    public void creatNotfication(String name, String iconUrl) {

    }

    /**
     * 刷新通知
     *
     * @param total   总的量
     * @param current 随时的量
     */
    public void refreshProgress(long total, long current, String state, boolean isEnd) {

        double x_double = current * 1.0;
        double tempresult = x_double / total;
        DecimalFormat df1 = new DecimalFormat("0.00"); // ##.00%
        // 百分比格式，后面不足2位的用0补齐
        String result = df1.format(tempresult);
        if (isEnd) {
            contentView.setTextViewText(R.id.notificationTitle, state);
            contentView.setTextViewText(R.id.notificationPercent, 100 + "%");
            contentView.setProgressBar(R.id.notificationProgress, 100, 100, false);
//        if (downloadStateEvent == null)
//            downloadStateEvent = new DownloadStateEvent();
//        EventBus.getDefault().post(downloadStateEvent.setCurrent((int) (Float.parseFloat(result) * 100)).setPosition(mPosition).setState(-1));
            notificationManager.notify(notification_id, notification);
            return;
        }
        contentView.setTextViewText(R.id.notificationPercent, (int) (Float.parseFloat(result) * 100) + "%");
        contentView.setProgressBar(R.id.notificationProgress, 100, (int) (Float.parseFloat(result) * 100), false);
//        if (downloadStateEvent == null)
//            downloadStateEvent = new DownloadStateEvent();
//        EventBus.getDefault().post(downloadStateEvent.setCurrent((int) (Float.parseFloat(result) * 100)).setPosition(mPosition).setState(-1));
        notificationManager.notify(notification_id, notification);
    }

    /**
     * 清除集合
     */
    public static void clearList() {
        if (!MapUtils.isEmpty(notificationUtilsList))
            notificationUtilsList.clear();
        notificationUtilsList = null;
    }
}
